package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.exception.RoleException;
import com.itcast.health.pojo.PageResult;
import com.itcast.health.pojo.QueryPageBean;
import com.itcast.health.pojo.Result;
import com.itcast.health.pojo.Role;
import com.itcast.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 新增角色
     *
     * @param role
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role) {
        try {
            roleService.add(role);
            return new Result(true, "新增角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加出错,请联系管理员或重试");
        }

    }

    /**
     * 查询所有权限
     * @return
     */
    @RequestMapping("/findAllPermission")
    public Result findAllPermission() {
        Result result = roleService.findAllPermission();
        if (result.isFlag()){
            return result;
        }
        return new Result(false,result.getMessage());
    }

    /**
     * 查询所有菜单
     * @return
     */
    @RequestMapping("/findAllMenu")
    public Result findAllMenu(){
        Result result = roleService.findAllMenu();
        if (result.isFlag()){
            return result;
        }
        return new Result(false,result.getMessage());
    }

    /**
     * 数据分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
       PageResult pageResult = roleService.findPage(queryPageBean);
       return pageResult;
    }

    /**
     * 编辑窗口 数据回显
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Role role = roleService.findById(id);
        return new Result(true,"",role);
    }

    /**
     * 编辑角色
     * @param role
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role){
        try {
            roleService.edit(role);
            return new Result(true,"编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑失败");
        }
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            return new Result(true,"删除成功");
        } catch (RoleException e) {
            e.printStackTrace();
            return new Result(false,"存在引用,无法删除");
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }
}
